<template>
	<!-- 修改境外人员 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传护照首页图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="passportIndexImg==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="passportIndexImg"></image>
			</view>
		</view>
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传签证图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage2">
				<image class="img" v-if="visaImg==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="visaImg"></image>
			</view>
		</view>
		<view class="add-content">
			<navigator class="add-item" url="../../Public/People" hover-class="none">
				<view class="item-left">境外人员<text>*</text></view>
				<view class="item-right">{{ userId_text }}</view>
			</navigator>
			<view class="add-item">
				<view class="item-left">国籍<text>*</text></view>
				<view class="item-right" @click="nationalityShow=true">{{ nationality_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">入境原因<text>*</text></view>
				<view class="item-right" @click="inboundShow=true">{{ inboundType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">入境时间<text>*</text></view>
				<view class="item-right" @click="show=true">{{ inboundDate }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">入境交通标号<text>*</text></view>
				<view class="item-right"><input v-model="inboundTrafficCode" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入入境交通标号" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">入境交通方式<text>*</text></view>
				<view class="item-right" @click="TrafficTypeShow=true">{{ inboundTrafficType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">护照编号<text>*</text></view>
				<view class="item-right"><input v-model="passportCode" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入护照编号" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">出境时间<text>*</text></view>
				<view class="item-right" @click="show2=true">{{ predictOutboundDate }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">签证编号<text>*</text></view>
				<view class="item-right"><input v-model="visaCode" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入签证编号" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">签证开始时间<text>*</text></view>
				<view class="item-right" @click="show3=true">{{ visaStartDate }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">签证到期时间<text>*</text></view>
				<view class="item-right" @click="show4=true">{{ visaEndDate }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">是否在境内<text>*</text></view>
				<view class="item-right" @click="jnShow=true">{{ isInDomestic_text }}</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 国籍 -->
		<u-select v-model="nationalityShow" :list="nationalityList" @confirm="confirmNationality"></u-select>
		<!-- 入境原因 -->
		<u-select v-model="inboundShow" :list="inboundList" @confirm="confirminbound"></u-select>
		<!-- 交通方式 -->
		<u-select v-model="TrafficTypeShow" :list="TrafficTypeList" @confirm="confirmTrafficType"></u-select>
		<!-- 入境时间 -->
		<u-picker mode="time" v-model="show" :params="params" @confirm="confirmInboundDate"></u-picker>
		<!-- 出境时间 -->
		<u-picker mode="time" v-model="show2" :params="params" @confirm="confirmInboundDate2"></u-picker>
		<!-- 签证开始时间 -->
		<u-picker mode="time" v-model="show3" :params="params" @confirm="confirmInboundDate3"></u-picker>
		<!-- 签证结束时间 -->
		<u-picker mode="time" v-model="show4" :params="params" @confirm="confirmInboundDate4"></u-picker>
		<!-- 是否在境内 -->
		<u-select v-model="jnShow" :list="jnList" @confirm="confirmJn"></u-select>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				jnShow:false,
				jnList:[
					{
						value:true,
						label:'是'
					},
					{
						value:false,
						label:'否'
					}
				],
				params: {
					year: true,
					month: true,
					day: true
				},
				show: false,
				show2:false,
				show3:false,
				show4:false,
				nationalityShow:false,
				inboundShow:false,
				TrafficTypeShow:false,
				TrafficTypeList:[],
				inboundList:[],
				inboundDate:'请选择',// 入境时间
				inboundTrafficCode:'',// 入境交通标号（飞机填写航班号，轮船填写航运号）
				inboundTrafficType:'', // 入境交通方式 
				inboundTrafficType_text:'请选择', // 入境交通方式 
				inboundType:'',// 入境原因
				inboundType_text:'请选择',
				isInDomestic:true,// 是否在境内
				isInDomestic_text:'是',
				nationality:'', //国籍
				nationality_text:'请选择', //国籍
				nationalityList:[],
				passportCode:'',//护照编号
				passportIndexImg:'',//护照首页图片
				predictOutboundDate:'请选择',//预计出境时间 yyyy-MM-d
				userId:'',
				userId_text:'请选择',
				visaCode:'',//签证编号
				visaEndDate:'请选择', // 签证到期时间
				visaImg:'',//签证图片
				visaStartDate:'请选择',//签证开始时间
				overseasId:''
			}
		},
		onLoad(options) {
			this.overseasId = options.id
		},
		mounted() {
			this.getDictionaries()
			this.getNationality()
			this.getInbound()
			this.getOverseasUserById()
		},
		onShow() {
			if(!uni.getStorageSync('people')) return
			let User = uni.getStorageSync('people')
			// console.log(User)
			this.userId_text = User.name
			this.userId = User.id
		},
		methods:{
			getOverseasUserById() {
				let params = {
					overseasId:this.overseasId
				}
				this.$u.api.getOverseasUserById(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							this.userId = res.result.userId
							this.userId_text = res.result.userName
							this.passportIndexImg = res.result.passportIndexImg
							this.visaImg = res.result.visaImg
							this.nationality = res.result.nationality
							this.nationalityList.forEach(item => {
								if(item.value == res.result.nationality) {
									this.nationality_text = item.label
								}
							})
							this.inboundType = res.result.inboundType
							this.inboundList.forEach(item => {
								if(item.value == res.result.inboundType) {
									this.inboundType_text = item.label
								}
							})
							this.inboundDate = res.result.inboundDate
							this.inboundTrafficCode = res.result.inboundTrafficCode
							this.inboundTrafficType = res.result.inboundTrafficType
							this.TrafficTypeList.forEach(item => {
								if(item.value == res.result.inboundTrafficType) {
									this.inboundTrafficType_text = item.label
								}
							})
							this.passportCode = res.result.passportCode
							this.predictOutboundDate = res.result.predictOutboundDate
							this.visaCode = res.result.visaCode
							this.visaStartDate = res.result.visaStartDate
							this.visaEndDate = res.result.visaEndDate
							this.isInDomestic = res.result.isInDomestic
							if(res.result.isInDomestic == true) {
								this.isInDomestic_text = '是'
							} else {
								this.isInDomestic_text = '否'
							}
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			confirmInboundDate(e) {
				this.inboundDate = e.year + '-' + e.month + '-' + e.day
			},
			confirmInboundDate2(e) {
				this.predictOutboundDate = e.year + '-' + e.month + '-' + e.day
			},
			confirmInboundDate3(e) {
				this.visaStartDate = e.year + '-' + e.month + '-' + e.day
			},
			confirmInboundDate4(e) {
				this.visaEndDate = e.year + '-' + e.month + '-' + e.day
			},
			confirmJn(e) {
				this.isInDomestic = e[0].value
				this.isInDomestic_text = e[0].label
			},
			// 国籍
			getNationality() {
				let params = {
					dictCode:'nationality_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.nationalityList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: err.message
						})
					})
			},
			confirmNationality(e) {
				console.log(e)
				this.nationality = e[0].value
				this.nationality_text = e[0].label
			},
			// 入境原因
			getInbound() {
				let params = {
					dictCode:'inbound_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.inboundList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: err.message
						})
					})
			},
			confirminbound(e) {
				this.inboundType = e[0].value
				this.inboundType_text = e[0].label
			},
			// 交通方式
			getDictionaries() {
				let params = {
					dictCode:'inbound_traffic_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.TrafficTypeList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: err.message
						})
					})
			},
			confirmTrafficType(e) {
				this.inboundTrafficType = e[0].value
				this.inboundTrafficType_text = e[0].label
			},
			
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.passportIndexImg = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 上传图片
			chooseImage2() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.visaImg = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 提交
			handleSubmit() {
				if(this.passportIndexImg == '') {
					return this.$refs.uToast.show({
						title: '请上传护照首页图片'
					})
				}
				if(this.visaImg == '') {
					return this.$refs.uToast.show({
						title: '请上传签证图片'
					})
				}
				if(this.userId == '') {
					return this.$refs.uToast.show({
						title: '请选择境外人员'
					})
				}
				if(this.nationality == '') {
					return this.$refs.uToast.show({
						title: '请选择国籍'
					})
				}
				if(this.inboundDate == '请选择') {
					return this.$refs.uToast.show({
						title: '请选择入境时间'
					})
				}
				if(this.inboundType == '') {
					return this.$refs.uToast.show({
						title: '请选择入境原因'
					})
				}
				if(this.inboundTrafficCode == '') {
					return this.$refs.uToast.show({
						title: '请填写入境交通标号'
					})
				}
				if(this.inboundTrafficType == '') {
					return this.$refs.uToast.show({
						title: '请选择入境交通方式'
					})
				}
				if(this.passportCode == '') {
					return this.$refs.uToast.show({
						title: '请填写护照编号'
					})
				}
				if(this.predictOutboundDate == '') {
					return this.$refs.uToast.show({
						title: '请选择出境时间'
					})
				}
				if(this.visaCode == '') {
					return this.$refs.uToast.show({
						title: '请填写签证编号'
					})
				}
				if(this.visaStartDate == '') {
					return this.$refs.uToast.show({
						title: '请选择签证开始时间'
					})
				}
				if(this.visaEndDate == '') {
					return this.$refs.uToast.show({
						title: '请选择签证到期时间'
					})
				}
				uni.removeStorageSync('people')
				let params = {
					inboundDate:this.inboundDate,
					inboundTrafficCode:this.inboundTrafficCode,
					inboundTrafficType:this.inboundTrafficType,
					inboundType:this.inboundType,
					isInDomestic:this.isInDomestic,
					nationality:this.nationality,
					passportCode:this.passportCode,
					passportIndexImg:this.passportIndexImg,
					predictOutboundDate:this.predictOutboundDate,
					userId:this.userId,
					visaCode:this.visaCode,
					visaEndDate:this.visaEndDate,
					visaImg:this.visaImg,
					visaStartDate:this.visaStartDate,
					id:this.overseasId
				}
				console.log(params)
				this.$u.api.editOverseasUser(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							this.$refs.uToast.show({
								title: '修改成功'
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: err.message
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
